package com.tank.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tank.bean.msg.PmContent;
import com.tank.bean.msg.PmTracker;
import com.tank.bean.User;
import com.tank.core.TankModel;
import com.tank.dao.PmDAO;
import com.tank.service.PmService;
import com.tank.util.TankUtil;

@Service("pmService")
public class PmServiceImpl implements PmService {

	@Resource(name = "pmDao")
	PmDAO dao;

	public TankModel trackers() {
		TankModel tm = TankModel.newInstance();
		tm.setAttribute("me", TankUtil.getCurrentUser());
		Integer actionId = TankUtil.getCurrentUser().getId();
		List<PmTracker> pts = dao.trackers(actionId);

		tm.setData(pts);

		return tm;
	}

	/**
	 * 该落实的实体：发送者id,接收者id,私信内容
	 */
	public TankModel send(PmContent pc) {
		TankModel tm = TankModel.newInstance();
		pc.setSender(TankUtil.getCurrentUser());

		if (pc.getReceiptor() == null) {
			tm.setSuccess(false);
			tm.setMessage("请求出错");
		} else if (pc.getReceiptor().getId() == null
				&& pc.getReceiptor().getName() == null) {
			tm.setSuccess(false);
			tm.setMessage("接收人不存在!");
		} else if (pc.getReceiptor().getName() != null) {
			pc = dao.buildUser(pc);
			if (pc.getSender().getId().equals(pc.getReceiptor().getId())) {
				tm.setSuccess(false);
				tm.setMessage("不能私信自己!");
			}else if(pc.getReceiptor().getId() == null){
				tm.setSuccess(false);
				tm.setMessage(pc.getReceiptor().getName() + "不存在!");
			}

		}
		System.out.println(pc.getContent());
		if (!tm.isSuccess())
			return tm;

		pc = dao.buildDialog(pc);// 构建对话

		pc = dao.buildPm(pc); // 构建私信息

		pc = dao.buildTracker(pc);// 构建跟踪器
		
		tm.setAttribute("pc", pc);
		
		return tm;
	}

	public TankModel dialog(Integer dialogId) {
		TankModel tm = TankModel.newInstance();
		Integer actionId = TankUtil.getCurrentUser().getId();
		// 查询指定对话下没被当前用户删除的内容
		List<PmContent> pcs = dao.dialog(actionId, dialogId);
		User to;
		//if(pcs.size()<1) {
			to = dao.dialogPartner(actionId, dialogId); 
			
			//System.out.println(to);
		//}else{
		//	to=pcs.get(0).getSender().getId()==actionId?pcs.get(0).getReceiptor():pcs.get(0).getSender();
		//}
		
		
		tm.setAttribute("to", to);
		tm.setAttribute("me", TankUtil.getCurrentUser());
		tm.setData(pcs);
		//tm.printData();
		// System.out.println(pcs.size());
		// 去更新追踪器id,dialogid
		// 去标记本次对话接收者已读id,dialogid
		dao.markRead(actionId, dialogId);

		return tm;
	}

	public Integer countUnread() {
		Integer actionId = TankUtil.getCurrentUser().getId();
		return dao.countUnread(actionId);
	}

	public boolean removeContent(Integer dialogId, Integer contentId) {
		Integer actionId = TankUtil.getCurrentUser().getId();
		dao.removeContent(actionId, dialogId, contentId);
		return true;
	}

	public boolean removeBatch(Integer dialogId) {
		Integer actionId = TankUtil.getCurrentUser().getId();
		dao.removeBatch(actionId, dialogId);
		return true;
	}

}
